package com.china08.yxyapi.api.manage.stu.mystudy.tifenbao.exercise;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.es.ElasticYxyQuestionBankResource;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnow;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.model.questionbank.YxyQuesBanQuestType1_2_3Model;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanQuesRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;
import com.china08.yxyapi.service.mg.impl.PlanExerciseServiceImpl;
import com.google.gson.Gson;

/**
 * 获取试题详细信息
 *
 */
@RestController
public class TifenbaoExerciseByOneController {

    @Autowired
    private PlanExerciseServiceImpl planExerciseServiceImpl;

    @Autowired
    private YxyHuixuePlanQuesRepository yxyHuixuePlanQuesRepository;

    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @Autowired
    private YxyHuixuePlanKnowRepository yxyHuixuePlanKnowRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/tifenbao/exercise/getone", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        reqModel.check();

        ElasticYxyQuestionBankResource question = planExerciseServiceImpl.getExerciseById(reqModel.quesId);

        RespModel model = new RespModel();
        if (question != null) {
            model.questionId = question.getId();
            model.questionBody = question.getQues();
            if (question.getQuesType().equals("1") || question.getQuesType().equals("2")
                    || question.getQuesType().equals("3")) {
                Gson gson = new Gson();
                YxyQuesBanQuestType1_2_3Model banQuestType1_2_3Model = gson.fromJson(question.getQuesBody(),
                        YxyQuesBanQuestType1_2_3Model.class);
                String answerTrue = banQuestType1_2_3Model.getQuesAnswer().getOptions();

                model.answer = answerTrue;
            }

            model.difficultyLevel = question.getDifficultyLevel();
            model.type = question.getQuesType();
            model.knowNum = question.getKnowNum().toString();
            model.knowIds = StringUtils.join(question.getKnowIds().toArray(), ",");
            model.stage = "编号：" + question.getStage() + "，名称：" + question.getStageName();

            YxyHuixuePlanKnow planKnow = yxyHuixuePlanKnowRepository.findTopByPkId(reqModel.planKnowId);

            YxyHuixuePlan plan = yxyHuixuePlanRepository.findOne(planKnow.getPlanId());

            YxyHuixuePlanQues planques = yxyHuixuePlanQuesRepository
                    .findByStudentIdAndPlanIdAndQuesId(plan.getStudentId(), plan.getId(), question.getId());

            if (planques != null) {
                model.ifquanzhong = "是";
                model.quanzhong = planques.getWeight().toString();
            } else {
                model.ifquanzhong = "否";
            }
        }
        return model;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 试题id
        public String quesId;

        public String planKnowId;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(quesId, null), "试题编号不能为空");
        }
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // 试题id
        public String questionId;

        // 试题body
        public Object questionBody;

        // ***********临时数据*************

        public String answer;

        public String difficultyLevel;

        public String type;

        public String stage;

        public String knowNum;

        public String knowIds;

        public String ifquanzhong;

        public String quanzhong;

    }
}
